<?php namespace App\Controllers;

use \App\App;

class Comment extends App
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $page = $this->request->post('page') ?: 0;
    }

    public function post()
    {
        $content = $this->request->post('content');
        $storyID = $this->request->post("id");

        if (!$this->user) {
            $this->showJson("用户尚未登录，请登录后重新发表。", 'Error');
        }
        $comment = [
            "content" => htmlspecialchars($content),
            "story" => $storyID,
            "user" => $this->user['id'],
            "date" => time()
        ];
        $commentModel = new \App\Models\Comment();
        $commentModel->save($comment);
        //echo $commentModel->lastQuery();
        $html = '<div class="comment"><a class="avatar">' .
            '<img src="' . site_url("/assets") . '/images/avatar/' . ($this->user['avatar'] ?: 'default.jpg') . "\"></a>" .
            '<div class="content">' . "<a class=\"author\">{$this->user['username']}</a>" .
            ' <div class="metadata">' .
            ' <span class="date">' . date("Y-m-d H:m", $comment['date']) . '</span>' .
            ' </div>        <div class="text">' .
            preg_replace("/:([a-z\-\_]+):/i", "<i class='em em-$1'></i>", $comment['content']) .
            '</div><div class="actions"><a class="reply"><i class="comment outline icon"></i> 回复</a></div></div> </div>';
        $this->showJson([
            "status" => "Success",
            "message" => "发表评论成功",
            "html" => $html
        ]);
    }
}
